export function connectSSE(
  // _this,
  // config = { url: "/strategy/analyze", params: {} },
  url,
  successCallback,
  errorCallback
) {
  // new URLSearchParams(params).toString()
  // const eventSource = new EventSource(
  //   `${
  //     config?.url || "/strategy/analyze"
  //   }?file_paths=${config?.params?.file_paths || ""}&urls=${
  //     config?.params?.urls || ""
  //   }&text_input=${config?.params?.text_input || ""}&ai_model=${
  //     config?.params?.ai_model || ""
  //   }`,
  //   { withCredentials: true }
  // );

  const eventSource = new EventSource(url);

  eventSource.addEventListener("result", function (event) {
    console.log("监听到了sse数据--", event);

    eventSource.close();
    if (typeof successCallback === "function") {
      successCallback(event.data);
    }
  });

  eventSource.onopen = (event) => {
    console.log("sse打开了");
  };

  eventSource.onerror = (error) => {
    eventSource.close();
    if (typeof errorCallback === "function") {
      errorCallback(error);
    }
  };

  function handleResData(res) {
    if (typeof res === "string") {
      return res;
    } else {
      let str = "";
      for (const key in res) {
        if (typeof res[key] === "string") {
          // 拼接图片
          if (res[key]?.includes(".jpg") || res[key]?.includes(".png")) {
            str += `<img src="${res[key]}" style="width: 100%;height: 100%;"><br>`;
          } else {
            str += `${res[key]}<br>`;
          }
        }
      }
      return str;
    }
  }
}